//
//  STTableBarController.m
//  01-weibo
//
//  Created by 王戈 on 14-7-3.
//  Copyright (c) 2014年 itcast. All rights reserved.
//
#import "STTableBarController.h"
#import "STNavigationController.h"
#import "CategoryViewController.h"
#import "MineViewController.h"
#import "DiscoverViewController.h"
#import "HomeViewController.h"
#import "TailViewController.h"

#import "STTabbar.h"

@interface STTableBarController ()

@end

@implementation STTableBarController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setCustomTabBar];
    
    [self addAllChildViewControllers];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationChangeTab:) name:kNotification_ChangeTab object:nil];
    
}

- (void)notificationChangeTab:(NSNotification *)notification
{
    if ([notification.userInfo objectForKey:@"cate"]) {
        for (UIViewController *vc in self.childViewControllers) {
            if ([vc.childViewControllers.firstObject isKindOfClass:[CategoryViewController class]])
            {
                CategoryViewController *cvc = (CategoryViewController *)[vc.childViewControllers objectAtIndex:0];
                cvc.currentCategoryID = [NSString stringWithFormat:@"%d",[[notification.userInfo objectForKey:@"cate"] intValue]];
            }
        }
        self.selectedIndex = [[notification.userInfo objectForKey:@"index"] intValue];
    }
    else if ([notification.userInfo objectForKey:@"disc"])
    {
        for (UIViewController *vc in self.childViewControllers) {
            if ([vc.childViewControllers.firstObject isKindOfClass:[DiscoverViewController class]])
            {
                DiscoverViewController *dvc = (DiscoverViewController *)[vc.childViewControllers objectAtIndex:0];
                dvc.nextNum = [[notification.userInfo objectForKey:@"disc"] intValue];
                dvc.nextVC = YES;
            }
        }
        self.selectedIndex = [[notification.userInfo objectForKey:@"index"] intValue];
    }
    else if ([notification.userInfo objectForKey:@"tail"])
    {
        self.selectedIndex = [[notification.userInfo objectForKey:@"index"] intValue];
    }
    else
    {
        self.selectedIndex = [[notification.userInfo objectForKey:@"index"] intValue];
    }
    
    
}

//- (void)viewWillAppear:(BOOL)animated
//{
//    [self setNeedsStatusBarAppearanceUpdate];
//}
//
//- (BOOL)prefersStatusBarHidden
//{
//    return YES;
//}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

#pragma mark 设置子控制器
// 添加各类自控制器
- (void)addAllChildViewControllers
{
    HomeViewController *home = [[HomeViewController alloc] init];
    [self addChildViewController:home Title:@"" imageName:@"index1" selectedImageName:@"index2"];
    
    CategoryViewController *category = [[CategoryViewController alloc]init];
    [self addChildViewController:category Title:@"分类" imageName:@"index3" selectedImageName:@"index4"];
    
    DiscoverViewController *discover = [[DiscoverViewController alloc]init];
    [self addChildViewController:discover Title:@"发现" imageName:@"index5" selectedImageName:@"index6"];
    
    TailViewController *tail = [[TailViewController alloc]init];
    [self addChildViewController:tail Title:@"尾货" imageName:@"index7" selectedImageName:@"index8"];
    
    MineViewController *profile = [[MineViewController alloc]init];
    [self addChildViewController:profile Title:@"我的" imageName:@"index9" selectedImageName:@"index10"];
}

// 设置子控制器的方法
- (void)addChildViewController:(UIViewController *)vc Title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
//    vc.title = title;
    
    
    
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    vc.tabBarItem.image = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    vc.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    
    // 设置字体主题
//    NSMutableDictionary *textAttribute = [NSMutableDictionary dictionary];
//    textAttribute[NSForegroundColorAttributeName] = [UIColor blackColor];
//    textAttribute[NSFontAttributeName] = [UIFont systemFontOfSize:10];
//    [vc.tabBarItem setTitleTextAttributes:textAttribute forState:UIControlStateNormal];
//    
//    NSMutableDictionary *textAttributeSelected = [NSMutableDictionary dictionary];
//    textAttributeSelected[NSFontAttributeName] = [UIFont systemFontOfSize:10];
//    textAttributeSelected[NSForegroundColorAttributeName] = [UIColor orangeColor];
//    [vc.tabBarItem setTitleTextAttributes:textAttributeSelected forState:UIControlStateSelected];
    
    
    
    STNavigationController *nc = [[STNavigationController alloc]initWithRootViewController:vc];
    [self addChildViewController:nc];
}

#pragma mark 设置自定义tabbar
// 设置自定义tabbar
- (void)setCustomTabBar
{
    self.tabBar.translucent = NO;
//    STTabbar *customTabbar = [[STTabbar alloc]init];
//    customTabbar.delegate1 = self;
//    [self setValue:customTabbar forKeyPath:@"tabBar"];
}
@end
